# File Name:
# Author:chenjiajun
# Date:2025/9/7


from model.server import Server
from flask_restful import Api,Resource
from flask import request
from lib.resp import responseFunc
from . import ser_bp
import socket

api = Api(ser_bp)

class ServerAPI(Resource):
    def get(self):
        keyword = request.args.get('keyword')
        status = request.args.get('status')
        if not keyword and not status:
            server = Server.query.all()
            counts = Server.query.count()
            sev = [dict(item) for item in server]
            data =dict(list=sev, total=counts)
        else:
            ret = Server.select_server(keyword, status)
            counts = Server.select_server_count(keyword, status)[0][0]
            sev=[]
            for item in ret:
                sev_id = item[0]
                sev_name = item[1]
                sev_ip = item[2]
                sev_port = item[3]
                sev_status = item[4]
                sev_remark = item[5]
                add_time = item[6].strftime("%Y-%m-%d %H:%M:%S")
                sev.append(dict(sev_id=sev_id, sev_name=sev_name,sev_ip=sev_ip,sev_port=sev_port,sev_status=sev_status,sev_remark=sev_remark,add_time=add_time))
            data = dict(list=sev, total=counts)
        return responseFunc(message='查询成功',code = 20000,data = data)

    def post(self):
        sev_name = request.json.get('sev_name')
        sev_ip = request.json.get('sev_ip')
        sev_port = request.json.get('sev_port')
        sev_status = request.json.get('sev_status')
        sev_remark = request.json.get('sev_remark')
        try:
            Server.create_server(sev_name, sev_ip, sev_port,sev_remark,sev_status)
            return responseFunc(message='添加服务器成功',code = 20000)
        except Exception as e:
            return responseFunc(message=f'添加服务器失败{str(e)}',code = 0)

    def put(self):
        data = request.json
        sev_id = data.get('sev_id')
        sev_name = request.json.get('sev_name')
        sev_ip = request.json.get('sev_ip')
        sev_port = request.json.get('sev_port')
        sev_status = request.json.get('sev_status')
        sev_remark = request.json.get('sev_remark')
        try:
            Server.modify_server(sev_id, sev_name, sev_ip, sev_port, sev_remark, sev_status)
            return responseFunc(message='修改成功',code = 20000)
        except Exception as e:
            return responseFunc(message=f'修改失败{str(e)}',code = 0)

    def delete(self):
        data = request.json
        Server.delete_server(data['sev_id'])
        return responseFunc(message='删除资源成功',code = 20000)

api.add_resource(ServerAPI, '/server')



class ServerTest(Resource):
    def get(self):
        ip = request.args.get('ip')
        port = int(request.args.get('port'))
        try:
            with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
                # 设置超时时间
                s.settimeout(5)
                # 尝试连接
                result = s.connect_ex((ip, port))
                # connect_ex返回0表示成功
            if result == 0:
                return responseFunc(message='连接测试成功', code=20000)
            else:
                return responseFunc(message='请检查IP和端口', code=0)
        except Exception as e:
                return responseFunc(code=0, message=f"请检查IP和端口: {str(e)}")

api.add_resource(ServerTest, '/servertest')